#!/bin/bash
# Synchronize front-end DB and back-end DB, update back-end collection tj_items, tj_cats_p and tj_cats_s using front-end collection items
#

if [ $# -lt 5 ]; then
	echo "ERROR Insuffcient arguments. Need 5 but got '$#'"
	echo "Usage: ./synchronize.sh </path/to/taobaoImporter/root/directory> <source database> <source collection> <destination database> <destination collection>"
	echo ""
	exit 1
fi

rootDir=$1;shift
src_db=$1;shift
src_coll=$1;shift
dst_db=$1;shift
dst_coll=$1;shift 

# verify the existence of the directories
if [ ! -e "${rootDir}" ]; then
	echo "ERROR importer item dir '${rootDir}' don't exist. Exit"
	echo "Usage: ./synchronize.sh </path/to/taobaoImporter/root/directory> <source database> <source collection> <destination database> <destination collection>"
	echo ""
	exit 1
fi

# trim the tailing '/' character
rootDir=`echo $rootDir | sed -e 's/\/$//'`
echo ${rootDir}

# construct the classpath
classpath=${rootDir}/release/taobao-importer-1.0.0.jar:${rootDir}/lib/commons-cli-1.2.jar:${rootDir}/lib/gson-1.7.1.jar:${rootDir}/lib/mongo-2.6.5.jar:${rootDir}/lib/taobao-sdk-java-20110804.jar:${rootDir}/lib/commons-lang3-3.0.1.jar:${rootDir}/lib/log4j-1.2.16.jar

# specify the main function
mainClass=com.googlecode.ulysses.taobao.synchronization.DBSynchronization
# import
#for i in tj_items tj_cats_p tj_cats_s
#do
#	dst_coll=$i

#	echo 
	echo Start Synchronize collection ${src_coll} to ${dst_coll}

	echo  java -cp ${classpath} ${mainClass}  -sd ${src_db} -sc ${src_coll} -dd ${dst_db} -dc ${dst_coll}
	java -cp ${classpath} ${mainClass}  -sd ${src_db} -sc ${src_coll} -dd ${dst_db} -dc ${dst_coll}  

	echo Done.
	echo ""
#done
